/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import pojo.KhachHang_pojo;
import util.MySQLDataAccessHelper;

/**
 *
 * @author Rocker000
 */
public class KhachHang_dao
{
    public static ArrayList<KhachHang_pojo> LayDanhSach()
    {
        ArrayList<KhachHang_pojo> ds = new ArrayList<KhachHang_pojo>();
        String sql ="select*from khachhang";
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                KhachHang_pojo kh = new KhachHang_pojo();
                kh.setMatKhau(rs.getString("matkhau"));
                kh.setTenKhachhang(rs.getString("tenkhachhang"));
                kh.setTenDangNhap(rs.getString("tendangnhap"));
                kh.setEmail(rs.getString("email"));
                kh.setDiaChi(rs.getString("diachi"));
                kh.setIsAdmin(rs.getInt("isAdmin"));
                kh.setSoDienThoai(rs.getString("sodienthoai"));
                ds.add(kh);
            }
            helper.Close();
        }
        catch(Exception ex)
        {}
        return ds;
    }
    
    public static KhachHang_pojo DangNhap(String ten,String pass)
    {        
        KhachHang_pojo kh = new KhachHang_pojo();
        String sql = String.format("select*from KhachHang where tendangnhap = '%s' and matkhau = '%s'",ten,pass);
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            if(rs.next())
            {
                kh.setMatKhau(rs.getString("matkhau"));
                kh.setTenKhachhang(rs.getString("tenkhachhang"));
                kh.setTenDangNhap(rs.getString("tendangnhap"));
                kh.setEmail(rs.getString("email"));
                kh.setDiaChi(rs.getString("diachi"));
                kh.setIsAdmin(rs.getInt("isAdmin"));
                kh.setSoDienThoai(rs.getString("sodienthoai"));
            }    
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return kh;
    }
    public static void CapNhatThongTin(String username,String tenthat,String email,String diachi,String sodienthoai)
    {
        String sql=String.format("update khachhang set tenkhachhang='%s',email='%s',diachi='%s',sodienthoai='%s' where tendangnhap='%s'",tenthat,email,diachi,sodienthoai,username);
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            helper.ExeCuteUpdate(sql);
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
    
    public static int ThayDoiMatKhau(String password)            
    {
        int kq = 1;
        String sql ="sel";
        return kq;
        
    }
    
    public static int KiemTraDangNhap(String tendangnhap, String matkhau)
    {
        int kq = 0;
        String sql = String.format("select*from KhachHang where tendangnhap = '%s' and matkhau = '%s'",tendangnhap,matkhau);
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            if(rs.next())
            {
                kq=1;
            }    
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return kq;
    }
    
    public static boolean ThemKhachHang(KhachHang_pojo x)
    {
        boolean kq= false;                
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            String sql = String.format("INSERT INTO `sieuthidienmay`.`khachhang` (`tendangnhap`, `matkhau`, `tenkhachhang`, `email`,`diachi`,`sodienthoai`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",x.getTenDangNhap(),x.getMatKhau(),x.getTenKhachhang(),x.getEmail(),x.getDiaChi(),x.getSoDienThoai());
            int n = helper.ExeCuteUpdate(sql);
            if(n==1)
                kq= true;            
            helper.Close();
        }
        catch(Exception ex)
        {
            System.err.println(ex.getMessage());
        }       
        return kq;
    }
    public static int  KiemTraTrungTen(String name)
    {
        int kq = 0;
        String sql = "select*from khachhang where tendangnhap='"+name+"'";
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            if(rs.next())
                kq=1;
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return kq;
    }
        
    
}
